/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.user.mail;

import hibernate.dao.MessageDAO;
import hibernate.entity.Accounts;
import hibernate.entity.Attachments;
import hibernate.entity.Conversations;
import hibernate.entity.Entities;
import hibernate.entity.MessageConversations;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import ui.common.AbstractUIBean;
import util.Converter;

/**
 *
 * @author Neo
 */
@ManagedBean
@RequestScoped
public class StaffInbox extends AbstractUIBean {

    /**
     * Creates a new instance of StaffInbox
     */
    MessageDAO mesDao = new MessageDAO();
    List<Conversations> listConver;
    List<Entities> allMessage;
    List<Entities> allAssignment;
    Accounts acc = getCurrentAccount();
    
    public List<Entities> getAllMessage() {
        return allMessage;
    }
    
    public void setAllMessage(List<Entities> allMessage) {
        this.allMessage = allMessage;
    }

    public List<Entities> getAllAssignment() {
        return allAssignment;
    }

    public void setAllAssignment(List<Entities> allAssignment) {
        this.allAssignment = allAssignment;
    }
    
    
    public StaffInbox() {
        allMessage = mesDao.readByReceiverID(getCurrentAccount().getAccountId());
        allAssignment = mesDao.readAssignmentByReceiverID(getCurrentAccount().getAccountId());
    }

    public List<Attachments> getAttachmentsList(Entities en) {
        try {
            return Converter.iteratorToList(en.getAttachmentses().iterator());
        } catch (Exception e) {
            return new ArrayList<Attachments>();
        }
    }

    public String toTrash(MessageConversations mc) {
        mesDao.receiverToTrash(mc);
        allMessage = mesDao.readByReceiverID(getCurrentAccount().getAccountId());
        return "inbox";
    }

    public String read(MessageConversations mc) {
        Entities e = mc.getEntities();
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.setAttribute("currMessage", e);
        session.setAttribute("currMC", mc);
        return "viewmessage";
    }
}
